<?php

declare(strict_types=1);

namespace App\Model\PssModel;
use App\Model\BaseModel\BasePssModel;
use App\Service\CommonDataService\ServerConfigDataService;
use App\Service\Code\Normal;

/**
 * 第三方业务对接配置文档
 * @author chenshubo
 *
 */
class ServiceConfig extends BasePssModel
{
    public $table = 'thirdparty_service_configure';
    const TYPE_PAY      = 1;//支付配置
    const TYPE_CHANNEL  = 2;//渠道配置
    const TYPE_BILL_WAY = 3;//快递配置
    
    protected $serviceTypeMapList = [
        self::TYPE_PAY      => '支付公司',
        self::TYPE_CHANNEL  => '电商平台',
        self::TYPE_BILL_WAY => '物流公司',
    ];
    
    /**
     * 获取第三方业务对接配置info
     * @param string $service_code
     * @param int $service_type
     * @param string $service_platform_value
     * @param boolean $is_single_field
     * @return mixed
     */
    public function getConfigByEventsValue( string $service_code , int $service_type ,string $service_platform_value, bool $is_single_field = true  )
    {
        $result = ServerConfigDataService::instance()->queryServiceConfig($service_code, $service_type, $service_platform_value,$is_single_field);
        if( $result['code'] != Normal::SUC or empty($result["data"])) {
            
            if( !isset($this->serviceTypeMapList[$service_type]) ) {
                throw new \Exception("三方业务配置类型不正确!");
            }
            
            $error = $this->serviceTypeMapList[$service_type];
            throw new \Exception("云仓三方配置未配置，请联系管理员到云仓后台【系统配置=>{$error}】授权！");
        }
        
        //新版本        
        return $result['data'];
        
//         //旧版本
//         $select = [
//             'service_platform_value',
//             'service_third_value',
//             'service_name'
//         ];
//         $where = [
//             'service_code'              => $service_code,
//             'service_type'              => $service_type,
//             'service_platform_value'    => $service_platform_value,
//         ];
        
//         $row = $this->info($where,$select);
//         if($is_single_field === false){
//             return $row;
//         }
//         return $row['service_third_value'] ?? '';
    }
}